#!/usr/bin/env python3
import asyncio
import sys
import os
from datetime import datetime
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.core.redis_client import redis_manager

async def fix_whitelist():
    # 正确的默认IP白名单
    default_ips = [
        {
            "ip": "127.0.0.1",
            "description": "本地回环地址",
            "added_at": datetime.now().isoformat()
        },
        {
            "ip": "localhost",
            "description": "本地主机名",
            "added_at": datetime.now().isoformat()
        },
        {
            "ip": "192.168.0.0/16",
            "description": "私有网络A类地址段",
            "added_at": datetime.now().isoformat()
        },
        {
            "ip": "10.0.0.0/8",
            "description": "私有网络B类地址段",
            "added_at": datetime.now().isoformat()
        },
        {
            "ip": "172.16.0.0/12",
            "description": "私有网络C类地址段",
            "added_at": datetime.now().isoformat()
        }
    ]
    
    # 创建正确的白名单数据
    whitelist_data = {
        "ips": default_ips,
        "created_at": datetime.now().isoformat(),
        "version": "1.0"
    }
    
    # 保存到Redis
    success = await redis_manager.set_json("settings:ip_whitelist", whitelist_data)
    
    if success:
        print("✅ IP白名单修复成功")
        print(f"已添加 {len(default_ips)} 个默认IP:")
        for ip_entry in default_ips:
            print(f"  - {ip_entry['ip']} ({ip_entry['description']})")
    else:
        print("❌ IP白名单修复失败")

if __name__ == "__main__":
    asyncio.run(fix_whitelist())
